package com.ccz.demo.design.chain;

import java.util.ArrayList;
import java.util.List;

public class FilterChain {

    // 过滤器列表，实现Filter接口的实现类
    private List<Filter> filters = new ArrayList<>();
    // 计数器
    private int index = 0;

    public FilterChain addFilter(Filter filter) {
        this.filters.add(filter);
        return this;
    }

    //向责任链中加入过滤器（多个）
    public FilterChain addFilters(List<Filter> filters)
    {
        this.filters.addAll(filters);
        return this;
    }

    public void doFilter(Rule rule, FilterChain chain) {
        if (index == filters.size()) {
            return;
        }
        Filter filter = filters.get(index);
        index++;
        filter.execute(rule, chain);
    }

}
